package testjava;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompressor {
    static final int BUFFER = 8192;

    private File zipFile;
    ZipOutputStream out;

    public ZipCompressor(String pathName) {
        zipFile = new File(pathName);
    }

    public void compress(String srcPathName, String basedir) {
        File file = new File(srcPathName);
        if (!file.exists()) throw new RuntimeException(srcPathName + "不存在！");
        try {
            if (null == out) {
                out = new ZipOutputStream(new FileOutputStream(zipFile));
            }
            compress(file, out, basedir);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void compress(File file, ZipOutputStream out, String basedir) {
        /* 判断是目录还是文件 */
        if (file.isDirectory()) {
            System.out.println("压缩：" + basedir + file.getName());
            this.compressDirectory(file, out, basedir);
        } else {
            System.out.println("压缩：" + basedir + file.getName());
            this.compressFile(file, out, basedir);
        }
    }

    /** 压缩一个目录 */
    private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
        if (!dir.exists()) return;

        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            /* 递归 */
            compress(files[i], out, basedir + dir.getName() + "/");
        }
    }

    /** 压缩一个文件 */
    private void compressFile(File file, ZipOutputStream out, String basedir) {
        if (!file.exists()) {
            return;
        }
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(basedir + file.getName());
            byte data[] = new byte[BUFFER];
            int count, write = 0;
            long crc32 = CrcUtil.crc32(file);
            entry.setSize(file.length());
            entry.setCrc(crc32);
            if (file.getName().endsWith("png")) {
                entry.setMethod(ZipEntry.STORED);
                entry.setTime(file.lastModified());
                entry.setCompressedSize(file.length());
            } else {
                Deflater def2 = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
                DeflaterOutputStream def = new DeflaterOutputStream(new CountOutputStream(), def2);
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    def.write(data, 0, count);
                }
                def2.finish();
                while (!def2.finished()) {
                    def2.deflate(data, 0, data.length);
                }
                entry.setCompressedSize(def2.getBytesWritten());
                def.close();
                bis.close();
                bis = new BufferedInputStream(new FileInputStream(file));
            }
            out.putNextEntry(entry);
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            bis.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void close() throws IOException {
        if (null != out) {
            out.close();
        }
    }

}